package com.weixing.mall.provider.support.excalservice;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.weixing.mall.provider.api.model.vo.ItemVo;
import com.weixing.mall.provider.api.service.ItemFeign;
import com.weixing.mall.provider.model.query.ProductStockQuery;
import com.weixing.mall.provider.model.vo.ProductStockListVo;
import com.weixing.mall.provider.service.IProductStockService;
import com.weixing.mall.provider.support.excaldata.ProductStockED;
import com.weixing.mall.util.SKUUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 出库单导入数据处理
 *
 * @date 2020-6-6
 */
public class InDepotBillDataListener extends AnalysisEventListener<ProductStockED> {
    private static final Logger LOGGER = LoggerFactory.getLogger(InDepotBillDataListener.class);
    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 5;

    List<ProductStockED> list = new ArrayList<ProductStockED>();

    /**
     * 业务数据组装
     */
    private List<ItemVo> returnList;

    /**
     * 业务参数
     */
    private List<ItemVo>  itemVoList;

    private  ItemFeign itemFeign;
    private InDepotBillDataListener() {

    }

    public InDepotBillDataListener(ItemFeign itemFeign) {
        this.itemFeign = itemFeign;
    }

    /**
     * 这个每一条数据解析都会来调用
     *
     * @param productStockED
     * @param analysisContext
     */
    @Override
    public void invoke(ProductStockED productStockED, AnalysisContext analysisContext) {
        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(productStockED));
        list.add(productStockED);
    }


    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        LOGGER.info("{}条数据，开始处理数据！", list.size());
        //返回数据收集
        List<ItemVo> loadList = Lists.newArrayList();
        //判断数据是否为空
        if(CollUtil.isNotEmpty(list)){
            //匹配数据  条件货号、款号、颜色、尺码必须相同
                //循环匹配数据
                Iterator<ProductStockED> it = list.iterator();
                while (it.hasNext()){
                    ProductStockED pse = it.next();
                    if(StrUtil.isEmpty(pse.getGoodsSn())
                            ||StrUtil.isEmpty(pse.getStyleNo())
                            ||StrUtil.isEmpty(pse.getColor())
                            ||StrUtil.isEmpty(pse.getSize())){
                        continue;

                    }
                    itemVoList=itemFeign.getBygoodSn(pse.getGoodsSn());
                    if(null!=itemVoList&&itemVoList.size()!=0) {
                        for (ItemVo iv : itemVoList) {
                            //数据条件校验任何参数必须有值
                            if (StrUtil.isEmpty(iv.getGoodsSn())
                                    || StrUtil.isEmpty(iv.getStyleNo())
                                    || StrUtil.isEmpty(iv.getProductSkuDesp())) {
                                continue;
                            }
                            String color = "";
                            String size = "";
                            //sku拆分颜色
                            String[] skuDesp = iv.getProductSkuDesp().split(",");
                            if (skuDesp.length == 2) {
                                color = SKUUtils.color2(iv.getProductSkuDesp());
                                size = SKUUtils.size2(iv.getProductSkuDesp());
                            } else {
                                continue;
                            }
                            //判断颜色尺码是否为空
                            if (StrUtil.isEmpty(color) || StrUtil.isEmpty(size)) {
                                continue;
                            }
                            //比对条件
                            if (pse.getGoodsSn().trim().equals(iv.getGoodsSn().trim())
                                    && pse.getStyleNo().trim().equals(iv.getStyleNo().trim())
                                    && pse.getColor().trim().equals(color.trim())
                                    && pse.getSize().trim().equals(size.trim())) {
                                //返回导入数量
                                iv.setStock(pse.getQuantity());
                                //匹配成功装载数据
                                loadList.add(iv);
                                //移除匹配成功数据
                                it.remove();
                                break;//跳出此循环
                            }
                        }
                    }
            }
        }
        returnList = loadList;
        LOGGER.info("{}条数据，数据封装处理完成 ", returnList.size());
    }

    /**
     * 获取处理结果数据
     */
    public List<ItemVo> getReturnList(){
        return this.returnList;
    }
}
